﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Boco.CommonLib;
using System.IO;
using Newtonsoft.Json;

namespace DataSync.Common
{
    class TaskConfig
    {
        private static string ConfigXml = @"Config\Tasks.xml";
        //private static string ConfigJsonFile = @"Config\Tasks.json";

        /// <summary>
        /// 判断当前是否调试
        /// </summary>
        /// <returns></returns>
        public static bool IsDebug()
        {
            var strconf = GetValueByKey("IsDebug");
            if (string.IsNullOrEmpty(strconf) || strconf.ToUpper() == "FALSE")
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// 是否启动任务控制
        /// 默认不控制
        /// </summary>
        /// <returns></returns>
        public static bool IsTaskRunControl()
        {
            var strconf = GetValueByKey("IsTaskRunControl");
            if (strconf.ToUpper() == "TRUE")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 获取配置项列表
        /// </summary>
        /// <returns></returns>
        public static List<ConfigInfo> GetSettings()
        {
            var CurConfig = new List<ConfigInfo>();

            //读取配置文件
            string path = System.AppDomain.CurrentDomain.BaseDirectory + ConfigXml;
            var configinfo = XElement.Load(path).Element("Settings").Elements("Item");
            foreach (var item in configinfo)
            {
                ConfigInfo it = new ConfigInfo();
                it.Key = item.Attribute("key").Value;
                it.Value = item.Attribute("value").Value;
                CurConfig.Add(it);
            }
            return CurConfig;
        }
        /// <summary>
        /// 获取配置值
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static string GetValueByKey(string Key)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + ConfigXml;
            var t = XElement.Load(path).Element("Settings").Elements("Item").FirstOrDefault(p => p.Attribute("key").Value.ToUpper() == Key.ToUpper());

            if (t != null)
            {
                return t.Attribute("value").Value;
            }
            return string.Empty;
        }

        public static XElement GetTaskBase()
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + ConfigXml;
            return XElement.Load(path).Element("Tasks");
        }
        /// <summary>
        /// 获取配置
        /// </summary>
        /// <param name="ServiceName"></param>
        /// <returns></returns>
        public static XElement GetTaskConfigById(string TaskId)
        {
            return GetTaskBase().Elements("Task").FirstOrDefault(p => p.Attribute("ID").Value == TaskId && p.Attribute("Enabled").Value.ToUpper() == "TRUE");
        }
        public static XElement GetTaskConfigByType(string TaskType)
        {
            return GetTaskBase().Elements("Task").FirstOrDefault(p => p.Attribute("Type").Value == TaskType && p.Attribute("Enabled").Value.ToUpper() == "TRUE");
        }

        public static IEnumerable<TaskConf> GetTasks()
        {
            List<TaskConf> tasks = new List<TaskConf>();
            try
            {
                var nodes = GetTaskBase();
                foreach (var item in nodes.Elements("Task"))
                {
                    tasks.Add(new TaskConf()
                    {
                        Id = item.GetAttribute("ID"),
                        Name = item.GetAttribute("Name"),
                        Type = item.GetAttribute("Type"),
                        Enabled = item.GetAttribute("Enabled").ToUpper() == "TRUE" ? true : false
                    });
                }
                return tasks.Where(p => p.Enabled);
            }
            catch (Exception ex)
            {
                throw new ApplicationException(string.Format("读取任务列表失败"), ex);
            }
        }

        /// <summary>
        /// 获取配置
        /// </summary>
        /// <param name="ServiceName"></param>
        /// <returns></returns>
        public static XElement GetTaskConfig(string TaskId)
        {
            return GetTaskConfigById(TaskId);
        }
    }


    public class TaskConf
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }

        public bool Enabled { get; set; }
    }

    public class ConfigInfo
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }
}
